/*
	PlAr is Platform Arena: a 2D multiplayer shooting game
	Copyright (c) 2010, Antonio Ragagnin <spocchio@gmail.com>
	All rights reserved.

	This file is licensed under the New BSD License.
*/
package plar.core;


import java.awt.Shape;
import java.io.Serializable;
import java.util.List;
import java.util.ArrayList;


/**
* SpriteSet manage a collection of {@link Sprite} classes, wich contains the grapical description of an {@link Element}.
*
* @author Antonio Ragagnin
*
*/


public class SpriteSet implements Serializable {
	private static final long serialVersionUID = 1L;
	/**
	* true if the client have to manage the size of the image of the {@link Sprite}.
	*/
	public boolean autoSize=true;
	/**
	* define the shape of the sprite.
	*/
	public Shape shape;
	
	List <Sprite> sprites;
	
	/**
	* the duration of the set of sprites, do not set it manually, it is automatically calcolated with the duration of the sprites.
	*/
	public float totalTime=0;
	/**
	* initialize an empty set.
	*/
	public SpriteSet()
	{
		sprites = new ArrayList<Sprite> ();
	}
	/**
	* add a sprite
	*/

	public void add(Sprite s)
	{
		sprites.add(s);
	}
	
	/**
	* add a sprite of null duration 
	* @param s path of the image.
	*/

	public void add(String s)
	{
		sprites.add(new Sprite(s,0));
	}
	
	
	/**
	* leave just a sprite defined by the parameter s
	* @param s path of the image.
	*/
	public void set(String s)
	{
		sprites.clear();
		add(s);
	}

	/**
	* get a sprit by the index
	*/
	public Sprite get (int n)
	{
		return sprites.get(n);
	}

	/**
	* get the sprite list.
	*/
	public List <Sprite> getSprites ()
	{
		return sprites;
	}
	/**
	* compute the total time duration of this spriteset, with the sprite inside.
	*/
	public void computeTotalTime()
	{
		for(Sprite s : sprites)
		{
			totalTime+=s.time;
		}
		//Common.info(1,"total time:"+totalTime);
	}

	/**
	* remove a sprite
	*/
	public void remove(int n)
	{
		sprites.remove(n);
	}
	/**
* really don't know why
*/

	public Sprite getFromIndex (int n,int d)
	{
		int i=0;
		if(totalTime==0) computeTotalTime();
		int  k = (int)(d*totalTime);
		if(k<=0) return get(0);

		n=n%k;
		for(Sprite s : sprites)
		{
			int j=i;
			i+=(int)(d*s.time);
			if(n>j && n<=i) return s;

		}
		
		
		return get(0);
	}
	
	/**
	* set the sizze of the sprites according to the size of the container {@link Element}
	*/
	public void updateSpriteSize(Element e) {
		for(Sprite s:sprites)
		{
			//Common.info(7, " hum "+e.getSize());
			s.sizex=e.getSize().x;
			s.sizey=e.getSize().y;
		}
		//shape =  Common.PolygonShape2awtShape(e.shape);


	}
	public int size() {

		return sprites.size();
	}
	public String toString() {
		if(sprites.size()>0)	return sprites.get(0).filename;
		else return "[]";
	}
	public boolean equals(SpriteSet s)
	{

		boolean x = (s.autoSize==autoSize && sprites.equals(s.sprites));
		//Common.info(7," spriteSet equal test from "+this.autoSize+" "+s.autoSize+", "+this.sprites.size()+" "+s.sprites.size()+" "+this+" "+s+"  is"+x);
		return x;
	}
}
